package edu.sctu.fengleilin.month06.day150610;

import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

/**
 * Created by user09 on 2015/6/10.
 */
public class ExampleHashMap {
    //用Map声明引用HashMap对象
    Map calendar = new HashMap<>();
    //将元素对加入到Map引用的对象当中
    public ExampleHashMap(String d[],String i[]){
        for (int x = 0;x < d.length;x ++){
            calendar.put(d[x],i[x]);
        }
    }

    public static void main(String[] args) {
        //待加入的数据
        String [] dates = {"10/31/01","01/01/01","03/05/01","02/04/01"};
        String [] items = {"halloween","New Years","Birthday","Anniversary"};
        //创建对象实例
        ExampleHashMap exampleHashMap = new ExampleHashMap(dates,items);
        //输出Map引用对象中的key和value对
        System.out.println("map = " + exampleHashMap.calendar);
        //将Map中的key/value对映射成set集合
        Set mappings = exampleHashMap.calendar.entrySet();
        System.out.println("object\t\t\tkey\t\tvalue");
        //通过Set集合对元素进行遍历，得到key和value
        for (Iterator i = mappings.iterator();i.hasNext();){
            Map.Entry me = (Map.Entry)i.next();
            Object ok = me.getKey();
            Object ov = me.getValue();
            /*
            第一种:
            Map map = new HashMap();
            Iterator iter = map.entrySet().iterator();
            while (iter.hasNext()) {
            Map.Entry entry = (Map.Entry) iter.next();
            Object key = entry.getKey();
            Object val = entry.getValue();
            }
            第二种
            Map map = new HashMap();
            Iterator iter = map.keySet().iterator();
            while (iter.hasNext()) {
            Object key = iter.next();
            Object val = map.get(key);
            }
             */
            System.out.printf(me + "\t");
            System.out.println(ok + "\t");
            System.out.println(ov);
        }
    }
}
